//
//  MessageViewController.m
//  LoanOne
//
//  Created by WangZhenyu on 2017/10/18.
//  Copyright © 2017年 WangZhenyu. All rights reserved.
//

#import "MessageViewController.h"
#import "MessageTableViewCell.h"
#import "MJRefresh.h"
#import "NoContentView.h"
#import "GlobalFunction.h"
#import "WebShowViewController.h"

@interface MessageViewController ()<UITableViewDelegate, UITableViewDataSource>{
    NSMutableArray              *contentMutArr;
    NSInteger                   pageNum;
}
@property (weak, nonatomic) IBOutlet UITableView *tableView;

@end

@implementation MessageViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"消息中心";
    if (self.foraml) {
        [self addBackBtn];
    }else{
        [self addBlackBackBtn];
    }
    
    
    pageNum = 0;
    contentMutArr = [NSMutableArray array];
    
    //集成上下拉刷新
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        // 进入刷新状态后会自动调用这个block
        pageNum = 0;
        [self requestContent:NO];
        
    }];
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        pageNum = ++pageNum;
        [self requestContent:NO];
    }];
    
    [self requestContent:YES];
    
    [self.tableView setFrame:CGRectMake(0, 64 + IS_iPhoneX_Top, SCREEN_WIDTH_DEVICE, SCREEN_HEIGHT_DEVICE - 64 - IS_iPhoneX_Top)];
    // Do any additional setup after loading the view from its nib.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

//请求数据
- (void)requestContent:(BOOL)alert{
    if (alert) {
        [self loadingHUDAlert:@"正在加载"];
    }
    NSMutableDictionary *dic = [[NSMutableDictionary alloc] init];
    [dic setObject:[NSNumber numberWithInteger:pageNum] forKey:@"page"];
    [dic setObject:[NSNumber numberWithInteger:20] forKey:@"size"];
    
    [[ServiceRequest sharedService] GET:@"messages" parameters:dic success:^(id responseObject) {
        [self hideHudAlert];
        if (pageNum == 0) {
            [self.tableView.mj_header endRefreshing];
            [contentMutArr removeAllObjects];
            [self.tableView.mj_footer resetNoMoreData];
        }else{
            [self.tableView.mj_footer endRefreshing];
        }
        NSArray *arr = responseObject[@"data"];
        [contentMutArr addObjectsFromArray:arr];
        if (contentMutArr.count == 0) {
            self.tableView.tableHeaderView = [[NoContentView alloc] initItemFrame:self.tableView.frame imageName:@"zwxx" title:@"暂无消息内容"];
        }else{
            self.tableView.tableHeaderView = nil;
        }
        if (pageNum > 0 && arr.count == 0) {
            [self.tableView.mj_footer endRefreshingWithNoMoreData];
        }
        [self.tableView reloadData];
        
    } failure:^(NSString *error, NSInteger code) {
        [self hideHudAlert];
        if (pageNum == 0) {
            [self.tableView.mj_header endRefreshing];
        }else{
            [self.tableView.mj_footer endRefreshing];
        }
        if (code == 0) {
            self.tableView.tableHeaderView = [[NoContentView alloc] initItemFrame:self.tableView.frame imageType:InfoImageType_NoNetWork title:error];
        }else if (code == 404){
            if (pageNum == 0) {
                [contentMutArr removeAllObjects];
                self.tableView.tableHeaderView = [[NoContentView alloc] initItemFrame:self.tableView.frame imageName:@"zwxx" title:@"暂无消息内容"];
                [self.tableView reloadData];
            }
            [self.tableView.mj_footer endRefreshingWithNoMoreData];
        }else{
            [self showHUDAlert:error];
        }
        NSLog(@"fail");
    }];
}

#pragma mark - UITableView datasource methods

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [contentMutArr count];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 230 + (SCREEN_SCALE_320 - 1) * 94;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"MessageCell";
    MessageTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (!cell) {
        cell = [[[NSBundle mainBundle] loadNibNamed:@"MessageTableViewCell" owner:self options:nil] lastObject];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    NSDictionary *dic = [contentMutArr objectAtIndex:indexPath.row];
    cell.titleLabel.text = [dic objectForKey:@"title"];
    cell.contentLabel.text = [dic objectForKey:@"description"];
    [cell.contentImage sd_setImageWithURL:[NSURL URLWithString:[dic objectForKey:@"image"]]];
    NSString *tag = [dic objectForKey:@"tag"];
    if (tag.length > 0) {
        cell.hotIcon.hidden = NO;
        cell.hotIcon.text = tag;
    }else{
        cell.hotIcon.hidden = YES;
    }
    cell.timeLabel.text = compareDate([[dic objectForKey:@"datetime"] floatValue]/1000);
    return cell;
}

#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:[tableView indexPathForSelectedRow] animated:YES];
    NSDictionary *dic = [contentMutArr objectAtIndex:indexPath.row];
    WebShowViewController *viewCon = [[WebShowViewController alloc] initWithNibName:@"WebShowViewController" bundle:nil];
    viewCon.urlStr = [dic objectForKey:@"url"];
    [self.navigationController pushViewController:viewCon animated:YES];
    
}
@end
